package com.pencilisland.pay.app.mp.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pencilisland.pay.api.bean.mp.app.MessageRequest;
import com.pencilisland.pay.api.bean.mp.app.MessageResponse;
import com.pencilisland.pay.api.bean.mp.app.QueryGoodsListRES;
import com.pencilisland.pay.api.entity.GoodsEntity;
import com.pencilisland.pay.api.entity.GoodsImgEntity;
import com.pencilisland.pay.api.service.CacheService;
import com.pencilisland.pay.app.mp.service.MBService;
/**
* 报文逻辑处理.查询商品信息
*
* @author breeze
*/
@Service("MB_queryGoodsList")
public class  QueryGoodsListMB implements MBService {

	@Autowired
	ObjectMapper objectMapper;
	@Autowired
	CacheService cacheService;

	@Override
	public MessageResponse process(MessageRequest request) {
		
		List<GoodsEntity> goodsList = cacheService.retrieveAllGoods();
		
		QueryGoodsListRES res= new QueryGoodsListRES(true,null,"OK");
		
		if(goodsList!=null&&!goodsList.isEmpty()){
			List<QueryGoodsListRES.ElementGoods> elementGoodsList = new ArrayList<QueryGoodsListRES.ElementGoods>();
			for(GoodsEntity goods:goodsList){
				QueryGoodsListRES.ElementGoods elementGoods = new QueryGoodsListRES.ElementGoods();
				elementGoods.setGoodsCode(goods.getCode());
				elementGoods.setName(goods.getName());
				elementGoods.setPrice(goods.getPrice());
				elementGoods.setRemark(goods.getRemark());
				elementGoods.setStock(goods.getStock());
				elementGoods.setFreePost(goods.getFreePost());
				if(goods.getImgDetails()!=null&&!goods.getImgDetails().isEmpty()){
					List<QueryGoodsListRES.ElementImgDetails> imgDetails = new ArrayList<QueryGoodsListRES.ElementImgDetails>();
					for(GoodsImgEntity goodsImg:goods.getImgDetails()){
						QueryGoodsListRES.ElementImgDetails element = new QueryGoodsListRES.ElementImgDetails();
						element.setUrl(goodsImg.getUrl());
						element.setType(goodsImg.getType());
						imgDetails.add(element);
					}
					elementGoods.setImgDetails(imgDetails);
				}
				
				elementGoodsList.add(elementGoods);
			}
			
			res.setGoodsList(elementGoodsList);
		}
		
		return res;
	}
	
}
